package com.quick.develop.flowable.controller.process;

import com.quick.develop.flowable.domain.BpmBaseDO;
import com.quick.develop.flowable.domain.vo.instance.BpmProcessInstanceCancelReqVO;
import com.quick.develop.flowable.domain.vo.instance.BpmProcessInstanceCreateReqVO;
import com.quick.develop.flowable.domain.vo.task.BpmTaskLogReqVO;
import com.quick.develop.framework.common.page.PageResult;
import com.quick.develop.framework.common.page.R;
import com.quick.develop.flowable.application.IBpmProcessInstanceApplication;
import com.quick.develop.flowable.domain.instance.BpmProcessInstanceMyPageReqVO;
import com.quick.develop.flowable.domain.instance.BpmProcessInstancePageItemRespVO;
import com.quick.develop.flowable.service.process.IBpmProcessInstanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @author junyuan.liu
 * @description: 
 * @date 2022/4/25 15:48
 */
@Api(tags = "流程管理后台 - 流程实例") 
@RestController
@RequestMapping("/process-instance")
@Validated
public class BpmProcessInstanceController  {
    @Resource
    private IBpmProcessInstanceService bpmProcessInstanceService;
    
    @Resource
    private IBpmProcessInstanceApplication bpmProcessInstanceApplication;

    @GetMapping("/myProInstPage")
    @ApiOperation("我发起的流程实例")
    public R<PageResult<BpmProcessInstancePageItemRespVO>> getMyProcessInstancePage(@Valid BpmProcessInstanceMyPageReqVO pageVo){
        return R.ok(bpmProcessInstanceService.getMyProcessInstancePage(pageVo));
    }

    @GetMapping("/getMyProcessCount")
    @ApiOperation("我发起的流程实例数量")
    public R<?> getMyProcessCount() {
        return R.ok(bpmProcessInstanceService.getMyProcessCount());
    }

    @PostMapping("/create-defid")
    @ApiOperation("新建流程实例-流程定义ID")
    public R<String> createProcessInstance(@Valid @RequestBody BpmProcessInstanceCreateReqVO createReqVO){
        return R.ok(bpmProcessInstanceService.createProcessInstanceById(createReqVO));
    }

    @PostMapping("/create-defkey")
    @ApiOperation("新建流程实例-流程定义key")
    public R<String> createProcessInstanceByKey(@Valid @RequestBody BpmProcessInstanceCreateReqVO createReqVO){
        return R.ok(bpmProcessInstanceService.createProcessInstanceByKey(createReqVO));
    }

    @GetMapping("/diagramView")
    @ApiOperation("获取流程图 后端整合好图片流")
    public void diagramViewByProInstId(@RequestParam("id") String processInstanceId,
                                       HttpServletResponse httpServletResponse) throws Exception{
        bpmProcessInstanceService.diagramViewByProInstId(httpServletResponse, processInstanceId);
    }

    @PostMapping(value = "/start-process")
    @ApiOperation(value = "启动流程",notes = "创建流程实例，完成第一个节点")
    public R<?> startProcess(@Valid @RequestBody BpmProcessInstanceCreateReqVO reqVO){
        return R.ok(bpmProcessInstanceApplication.startProcess(reqVO));
    }

    @GetMapping(value = "/get-process-all-log")
    @ApiOperation(value = "获取所有流程日志",notes = "获取所有的日志，包含主子流程")
    public R<?> getProcessAllLog(BpmTaskLogReqVO reqVO) {
        return R.ok(bpmProcessInstanceApplication.getProcessLogs(reqVO));
    }
    
    @GetMapping(value = "/get-log-by-processInstanceId/{processInstanceId}")
    @ApiOperation(value = "获取流程日志",notes = "根据流程实例ID获取流程日志")
    public R<?> getLogByProcessInstanceId(@PathVariable("processInstanceId")String id) {
        return R.ok(bpmProcessInstanceApplication.getLogsByProcessInstanceId(id));
    }

    @GetMapping(value = "/get-processes-by-businessKey/{businessKey}")
    @ApiOperation(value = "获取流程实例",notes = "根据业务ID获取所有的流程实例")
    public R<?> getProcessInstancesByBusinessKey(@PathVariable("businessKey") String businessKey){
        return R.ok(bpmProcessInstanceService.getProcessInstancesByBusinessKey(businessKey));
    }

    @GetMapping(value = "/find-parent-processes")
    @ApiOperation(value = "获取父流程信息",notes = "获取所有父流程，不分级")
    public R<?> findParentProcesses(BpmBaseDO reqVo) {
        return R.ok(bpmProcessInstanceService.findParentProcesses(reqVo));
    }
    
    @GetMapping(value = "/get-processes-include-super")
    @ApiOperation(value = "获取流程信息",notes = "获取所有流程信息，本流程以及父流程")
    public R<?> getProcessesIncludeSuper(BpmBaseDO reqVo) {
        return R.ok(bpmProcessInstanceService.getProcessesIncludeSuper(reqVo));
    }

    @GetMapping(value = "/get-main-process-by-businessKey/{businessKey}")
    @ApiOperation(value = "获取主流程实例",notes = "根据业务ID获取主流程流程实例信息，只有一条")
    public R<?> getMainProcessInstanceByBusinessKey(@PathVariable("businessKey") String businessKey) {
        return R.ok(bpmProcessInstanceApplication.getMainProcessInstanceByBusinessKey(businessKey));
    }

    @GetMapping(value = "/get-main-process-by-processInstanceId/{id}")
    @ApiOperation(value = "获取主流程实例",notes = "根据流程实例ID获取主流程流程实例信息，只有一条")
    public R<?> getMainProcessInstanceByProcessInstanceId(@PathVariable("id") String id) {
        return R.ok(bpmProcessInstanceApplication.getMainProcessInstanceByProcessInstanceId(id));
    }
    
    @DeleteMapping(value = "/cancel")
    @ApiOperation(value = "终止流程实例", notes = "只能终止自己发起的")
    public R<?> cancelInstance(@Valid @RequestBody BpmProcessInstanceCancelReqVO reqVO) {
        bpmProcessInstanceApplication.cancelProcessInstance(reqVO);
        return R.ok();
    }

    @PostMapping(value = "/update-process-name-by-instances-id")
    @ApiOperation(value = "更新流程名称",notes = "根据流程实例ID更新流程名称")
    public R<?> updateProcessNameByProcessInstanceId(@RequestBody BpmProcessInstanceCreateReqVO reqVO){
        bpmProcessInstanceService.updateProcessNameByInstancesId(reqVO.getProcessInstanceId(),reqVO.getProcessName());
        return R.ok();
    }

    @PostMapping("/get-doc-process-data")
    @ApiOperation("获取业务查询的流程信息")
    public R<List<BpmProcessInstancePageItemRespVO>> getDocProcessData(@RequestBody List<String> businessKeys) {
        return R.ok(bpmProcessInstanceService.getDocProcessData(businessKeys));
    }


}
